#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QScreen>
#include <QDebug>
#include <QToolTip>
#include <QPixmap>
#include <QList>
#include <vector>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(triggered()),this,SLOT(on_pushButton_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

QImage Binaryzation(const QImage &origin)
{
    int width = origin.width();
    int height = origin.height();
    QImage newImg = QImage(width, height, QImage::Format_RGB888);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int gray = qGray(origin.pixel(x, y));
            int newGray;
            if (gray > 128)
                newGray = 255;
            else
                newGray = 0;
            newImg.setPixel(x, y, qRgb(newGray, newGray, newGray));
        }
    }
    return newImg;
}


QImage Vertical(const QImage &origin)
{
    QImage newImage(QSize(origin.width(), origin.height()), QImage::Format_ARGB32);
    QColor tmpColor;
    int r, g, b;
    for (int x = 0; x < newImage.width(); x++) {
        for (int y = 0; y < newImage.height(); y++) {
            tmpColor = QColor(origin.pixel(x, y));
            r = tmpColor.red();
            g = tmpColor.green();
            b = tmpColor.blue();

            newImage.setPixel(x, newImage.height() - y - 1, qRgb(r, g, b));

        }
    }
    return newImage;
}

QImage Horizontal(const QImage &origin)
{
    QImage newImage(QSize(origin.width(), origin.height()), QImage::Format_ARGB32);
    QColor tmpColor;
    int r, g, b;
    for (int x = 0; x < newImage.width(); x++) {
        for (int y = 0; y < newImage.height(); y++) {
            tmpColor = QColor(origin.pixel(x, y));
            r = tmpColor.red();
            g = tmpColor.green();
            b = tmpColor.blue();

            newImage.setPixel(newImage.width() - x - 1, y, qRgb(r, g, b));

        }
    }
    return newImage;
}

QImage ContourExtraction(const QImage &img)
{
    int width = img.width();
    int height = img.height();
    int pixel[8];
    QImage binImg = Binaryzation(img);
    QImage newImg = QImage(width, height, QImage::Format_RGB888);
    newImg.fill(Qt::white);

    uint8_t *rgb = newImg.bits();
    uint8_t *binrgb = binImg.bits();
    int nRowBytes = (width * 24 + 31) / 32 * 4;
    int  lineNum_24 = 0;
    for (int y = 1; y < height - 1; y++) {
        for (int x = 1; x < width - 1; x++) {
            memset(pixel, 0, 8);
            lineNum_24 = y * nRowBytes;
            if (binrgb[lineNum_24 + x * 3] == 0) {
                rgb[lineNum_24 + x * 3] = 0;
                rgb[lineNum_24 + x * 3 + 1] = 0;
                rgb[lineNum_24 + x * 3 + 2] = 0;
                pixel[0] = binrgb[(y - 1) * nRowBytes + (x - 1) * 3];
                pixel[1] = binrgb[(y) * nRowBytes + (x - 1) * 3];
                pixel[2] = binrgb[(y + 1) * nRowBytes + (x - 1) * 3];
                pixel[3] = binrgb[(y - 1) * nRowBytes + (x) * 3];
                pixel[4] = binrgb[(y + 1) * nRowBytes + (x) * 3];
                pixel[5] = binrgb[(y - 1) * nRowBytes + (x + 1) * 3];
                pixel[6] = binrgb[(y) * nRowBytes + (x + 1) * 3];
                pixel[7] = binrgb[(y + 1) * nRowBytes + (x + 1) * 3];

                if (pixel[0] + pixel[1] + pixel[2] + pixel[3] + pixel[4] + pixel[5] + pixel[6] + pixel[7] == 0) {
                    rgb[lineNum_24 + x * 3] = 255;
                    rgb[lineNum_24 + x * 3 + 1] = 255;
                    rgb[lineNum_24 + x * 3 + 2] = 255;
                }

            }
        }
    }
    return newImg;
}


void MainWindow::on_pushButton_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(image));
            }
        }
}


void MainWindow::on_pushButton_2_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(Vertical(image)));
            }
        }
}


void MainWindow::on_pushButton_3_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(Horizontal(image)));
            }
        }
}


void MainWindow::on_pushButton_4_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->image->setScaledContents(true);
                ui->image->setPixmap(QPixmap::fromImage(ContourExtraction(image)));
            }
        }
}

